package leihua.crm.workbench.web.controller;

import leihua.crm.commons.contants.Contants;
import leihua.crm.commons.domain.ReturnObject;
import leihua.crm.commons.utils.DateUtils;
import leihua.crm.commons.utils.UUIDUtils;
import leihua.crm.settings.domain.User;
import leihua.crm.settings.service.UserService;
import leihua.crm.workbench.domain.Contacts;
import leihua.crm.workbench.domain.Customer;
import leihua.crm.workbench.domain.CustomerRemark;
import leihua.crm.workbench.domain.Tran;
import leihua.crm.workbench.service.ContactsService;
import leihua.crm.workbench.service.CustomerRemarkService;
import leihua.crm.workbench.service.CustomerService;
import leihua.crm.workbench.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author leiHua
 * @Date 2022/10/28 12:24
 */
@Controller
public class CustomerController {
    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerRemarkService customerRemarkService;
    @Autowired
    TranService tranService;
    @Autowired
    ContactsService contactsService;
    @Autowired
    UserService userService;



    @RequestMapping("/workbench/customer/index.do")
    public  String index(HttpServletRequest request){
        //获取所有用户
        List<User> userList = userService.queryAllUsers();
        request.setAttribute("userList",userList);
        return"workbench/customer/index";
    }


    @RequestMapping("/workbench/customer/queryCustomerForPageByCondition.do")
    @ResponseBody
    public  Object queryCustomerForPageByCondition(String name,String owner,String website,String phone,int pageSize,int pageNo){
        ReturnObject returnObject = new ReturnObject();
        int beginNo=(pageNo-1)*pageSize;
        HashMap<String, Object> map = new HashMap<>();
        map.put("name",name);
        map.put("owner",owner);
        map.put("website",website);
        map.put("phone",phone);
        map.put("pageSize",pageSize);
        map.put("beginNo",beginNo);

        try {
            List<Customer> customerList = customerService.queryCustomerForPageByCondition(map);
            int totalRows = customerService.queryCountOfCustomerForPageByCondition(map);
            HashMap<String, Object> data = new HashMap<>();
            data.put("customerList",customerList);
            data.put("totalRows",totalRows);
            returnObject.setRetData(data);
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        } catch (Exception e) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("查询失败，请联系管理员");
            e.printStackTrace();
        }
        return  returnObject;

    }

    //跳转到详情页面
    @RequestMapping("/workbench/customer/queryCustomerForDetail.do")
    public String queryCustomerForDetail(String customerId, HttpServletRequest request){
        Customer customer = customerService.queryCustomerForDetailByCustomerId(customerId);
        List<CustomerRemark> remarkList = customerRemarkService.queryCustomerRemarkByCustomerId(customerId);
        List<Tran> tranList = tranService.queryTranOfCustomerByCustomerId(customerId);
        ResourceBundle possibility = ResourceBundle.getBundle("possibility");
        //交易成功的可能性
        for (Tran tran : tranList) {
            String s = possibility.getString(tran.getStage());
            tran.setPossibility(s);
        }
        List<Contacts> contactsList = contactsService.queryContactsOfCustomerByCustomerId(customerId);
        request.setAttribute("customer",customer);
        request.setAttribute("remarkList",remarkList);
        request.setAttribute("tranList",tranList);
        request.setAttribute("contactsList",contactsList);
        return"workbench/customer/detail";
    }

    //新增一条 客户备注信息
    @RequestMapping("/workbench/customer/saveCreateCustomerRemark.do")
    @ResponseBody
    public  Object saveCreateCustomerRemark(String customerId, String noteContent, HttpSession session){
        User user=(User)session.getAttribute(Contants.SESSION_USER);
        CustomerRemark customerRemark = new CustomerRemark();
        ReturnObject returnObject = new ReturnObject();
        try {
            customerRemark.setId(UUIDUtils.getUUID());
            customerRemark.setCustomerId(customerId);
            customerRemark.setCreateBy(user.getId());
            customerRemark.setNoteContent(noteContent);
            customerRemark.setCreateTime(DateUtils.formateDateTime(new Date()));
            customerRemark.setEditFlag(Contants.REMARK_DEIT_FLAG_NO_EDITED);
            customerRemarkService.saveCustomerRemark(customerRemark);
            returnObject.setRetData(customerRemark);
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        } catch (Exception e) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            e.printStackTrace();
        }
        return  returnObject;
    }

    //创建一个客户
    @RequestMapping("/workbench/customer/saveCreateCustomer.do")
    @ResponseBody
    public Object saveCreateCustomer(Customer customer,HttpSession session){
        ReturnObject returnObject = new ReturnObject();
        User user = (User) session.getAttribute(Contants.SESSION_USER);
        customer.setCreateTime(DateUtils.formateDateTime(new Date()));
        customer.setCreateBy(user.getId());
        customer.setId(UUIDUtils.getUUID());
        try {
            int i = customerService.saveCreateCustomer(customer);
            if (i>0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("创建失败，请联系管理员");
            }
        } catch (Exception e) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("创建失败，请联系管理员");
            e.printStackTrace();
        }
        return returnObject;
    }

    //根据id查询一个用户
    @RequestMapping("/workbench/customer/queryCustomerByCustomerId.do")
    @ResponseBody
    public Object queryCustomerByCustomerId(String customerId){
        Customer customer=customerService.queryCustomerByPrimaryKey(customerId);
        return customer;
    }

    @RequestMapping("/worckbench/customer/saveEditCustomer.do")
    @ResponseBody
    public Object saveEditCustomer(Customer customer,HttpSession session){
        User user= (User) session.getAttribute(Contants.SESSION_USER);
        String code="1";
        try {
            customer.setEditBy(user.getId());
            customer.setEditTime(DateUtils.formateDateTime(new Date()));
            int i=customerService.editCustomer(customer);
            if (i<0){
                code="0";
            }
        } catch (Exception e) {
            code="0";
            e.printStackTrace();
        }
        return code;
    }

    @RequestMapping("/workbench/customer/deleteCustomerByIds.do")
    @ResponseBody
    public Object deleteCustomerByIds(String []id){
        int i = customerService.deleteCustomerByIds(id);
        return i;
    }

}
